<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="margin-bottom: 20px">
      <el-button type="primary" size="medium"
      @click="$router.push('/sys-admin/permission/admin/add-new')">添加管理员</el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="180" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
      <el-table-column label="查看图片" width="100" align="center">
        <el-button size="mini">查看</el-button>
      </el-table-column>
       <!-- scope；封装了每行数据，scope.row代表点击按钮所在行的数据  -->

        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="openEditDialog(scope.row)" circle></el-button>
            <el-button type="danger" @click="openDeleteConfirm(scope.row)" size="mini" icon="el-icon-delete" circle></el-button>
          </template>
        </el-table-column>

    </el-table>

    <div style="text-align: right;margin: 10px auto">
      <!-- @current-change,页码改变时触发     -->
      <el-pagination
          @current-change="changPage"
          layout="total,prev,pager,next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>

    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" :rules="rules" ref="ruleForm">
        <el-form-item label="相册名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="相册描述" :label-width="formLabelWidth" prop="description">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth" prop="sort">
          <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      total:0,
      pageSize:0,
      currentPage:1,
      dialogFormVisible: false,
      editForm: {
        id:'',
        name: '',
        description: '',
        sort:''
      },
      formLabelWidth: '120px',
      rules: {
        name: [
          { required: true, message: '请输入相册名', trigger: 'blur' },
          { min: 2, max: 25, message: '长度在 2 到25 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入序号', trigger: 'blur' },
          { pattern:/^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/,message: '排序序号必须是0~255之间的数字',trigger:'blur'}
        ]
      }
    }
  },
  methods:{
    submitEditForm(){
      let url = "http://localhost:9180/album/update";
      //使用qs框架将js对象转为formData数据格式
      let formData = this.qs.stringify(this.editForm);
      this.axios.post(url,formData).then((response) =>{
        let jsonResult = response.data;
        if(jsonResult.state==20000){
          this.$message.success("修改相册成功");
          this.dialogFormVisible = false;//隐藏对话框
          this.loadAlbumList();//修改后刷新相册列表
        }else if(jsonResult.state==40400) {
            this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;//隐藏对话框
              this.loadAlbumList();//数据不存在，刷新相册列表
            }
          });
        }else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;//隐藏对话框
            }
          });
        }
      })
    },
    openEditDialog(album){
      let url = "http://localhost:9180/album/standard?id="+album.id;
      this.axios.get(url).then((response)=>{
        let jsonResult = response.data;
        if(jsonResult.state==20000){
          this.editForm = jsonResult.data;
          this.dialogFormVisible=true;//对话框显示出来
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    openDeleteConfirm(album){
      this.$confirm('此操作将永久删除['+album.name+']相册, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(album);
      }).catch(() => {

      });
    },
    handleDelete(album){
      let url = "http://localhost:9180/album/delete";
      //使用qs框架将js对象转为formData数据格式
      let formData = "albumId="+album.id;
      this.axios.post(url,formData).then((response) =>{
        let jsonResult = response.data;
        if(jsonResult.state==20000){
          this.$message.success("删除相册成功");
          //刷新列表
          this.loadAlbumList();
        }else if( jsonResult.state==40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          //刷新列表
          this.loadAlbumList();
        }else if(jsonResult.state==40900){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },

      changPage(value){//value代表当前页码值
        this.$router.replace('?page='+value)//给浏览器地址栏路径上添加page值
        this.loadAlbumList();
      },
      loadAlbumList(){
          //获取浏览器路径上的page值
          let page = this.$router.currentRoute.query.page;
          if(!page){//防止不通过点击页码，直接进入相册列表，默认查第一页
            page=1;
          }
         let url = "http://localhost:9180/album/list?page="+page;
         this.axios.get(url).then((response)=>{
             let jsonResult = response.data;
             if(jsonResult.state==20000){
               this.tableData = jsonResult.data.list;
               this.total = jsonResult.data.total;
               this.pageSize = jsonResult.data.pageSize;
               this.currentPage = jsonResult.data.currentPage;
             }else{
               this.$alert(jsonResult.message, '错误', {
                 confirmButtonText: '确定',
                 callback: action => {
                 }
               });
             }
         })
      }
   },
  created() {

  },
  mounted() {
     this.loadAlbumList();
  }
}
</script>





<style scoped>

</style>